package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"rk155/models/mysql"

	"rk155/rk155rpc/internal/svc"
	"rk155/rk155rpc/rk155rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateLogic) Update(in *rk155rpc.UpdateRequest) (*rk155rpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line

	update := mysql.User{
		Model:      gorm.Model{ID: uint(in.Id)},
		Tel:        in.Tel,
		Password:   in.Password,
		HeadImg:    in.HeadImg,
		UserId:     int(in.UserId),
		WxChatName: in.WxChatName,
	}

	err := mysql.DB.Debug().Model(&update).Updates(&update).Error

	if err != nil {
		return nil, fmt.Errorf("修改失败")
	}

	return &rk155rpc.UpdateResponse{Id: int64(update.ID)}, nil
}
